package qc.module.platform.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import qc.common.core.exception.QCPromptException;
import qc.module.platform.dto.dbTableSql.DBTableSqlDto;
import qc.module.platform.dto.dbTableSql.DBTableSqlSimpleDto;
import qc.module.platform.service.DbTableSqlService;

import java.util.List;

/**
 * DbTableSqlController
 *
 * @author QuCheng Tech
 * @since 2024/1/29
 */
@RestController
@RequestMapping("/dbtablesql")
public class DbTableSqlController {
    private DbTableSqlService service;

    @Autowired
    public void setDbTableSqlService(DbTableSqlService service) {
        this.service = service;
    }

    /***
     * 获取指定表的所有sql，不管状态
     *
     * @param id 表编码
     * @return java.util.List<qc.module.platform.dto.dbTableSql.DBTableSqlDto>
     * @author QuCheng Tech
     * @since 2024/1/29
     */
    @GetMapping("/table")
    public List<DBTableSqlDto> getTableAllByParam(@RequestParam(name = "id", required = true) String id) throws QCPromptException {
        return service.getTableAllSqls(id, false);
    }

    /***
     * 获取指定表的所有sql，不管状态
     *
     * @param id 表编码
     * @return java.util.List<qc.module.platform.dto.dbTableSql.DBTableSqlDto>
     * @author QuCheng Tech
     * @since 2024/1/29
     */
    @GetMapping("/table/{id}")
    public List<DBTableSqlDto> getTableAllByPath(@PathVariable(name = "id", required = true) String id) throws QCPromptException {
        return service.getTableAllSqls(id, false);
    }

    /***
     * 获取表所有sql简要信息，要求状态为正常
     *
     * @param id 表编码
     * @return java.util.List<qc.module.platform.dto.dbTableSql.DBTableSqlSimpleDto>
     * @author QuCheng Tech
     * @since 2024/1/29
     */
    @GetMapping("/tablesimple")
    public List<DBTableSqlSimpleDto> getTableAllSimpleByParam(@RequestParam(name = "id", required = true) String id) throws QCPromptException {
        return service.getTableAllSqlSimples(id, true);
    }

    /***
     * 获取表所有sql简要信息，要求状态为正常
     *
     * @param id 表编码
     * @return java.util.List<qc.module.platform.dto.dbTableSql.DBTableSqlSimpleDto>
     * @author QuCheng Tech
     * @since 2024/1/29
     */
    @GetMapping("/tablesimple/{id}")
    public List<DBTableSqlSimpleDto> getTableAllSimpleByPath(@PathVariable(name = "id", required = true) String id) throws QCPromptException {
        return service.getTableAllSqlSimples(id, true);
    }

    /***
     * 获取指定表的指定sql信息
     *
     * @param tbcode 表编码
     * @param code sql代码
     * @return qc.module.platform.dto.dbTableSql.DBTableSqlDto
     * @author QuCheng Tech
     * @since 2024/1/29
     */
    @GetMapping("/get")
    public DBTableSqlDto get(@RequestParam(name = "tbcode", required = true) String tbcode, @RequestParam(name = "code", required = true) String code) {
        return service.get(tbcode, code);
    }

    /**
     * 新增
     * @param dto
     * @return 成功返回null，失败返回错误信息
     * @author QuCheng Tech
     * @since 2024/3/13
     */
    @RequestMapping(value = "/add",method = {RequestMethod.GET,RequestMethod.POST})
    public String add(@RequestBody DBTableSqlDto dto){
        return service.add(dto);
    }

    /**
     * 修改
     * @param dto
     * @return 成功返回null，失败返回错误信息
     * @author QuCheng Tech
     * @since 2024/3/13
     */
    @RequestMapping(value = "/update",method = {RequestMethod.POST,RequestMethod.PUT})
    public String update(@RequestBody DBTableSqlDto dto){
        return service.update(dto);
    }

    /**
     * 删除
     * @param tbcode 数据库表编码
     * @param code sql语句编码
     * @return 成功返回null，失败返回错误信息
     * @author QuCheng Tech
     * @since 2024/3/13
     */
    @DeleteMapping("/delete")
    public String delete(@RequestParam String tbcode,@RequestParam String code) throws QCPromptException {
        return service.delete(tbcode,code);
    }
}
